﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LexLib.LexerNameSpace;
using System.IO;
using LexLib.LexemNameSpace;

namespace KeyWordRedactor
{
    public partial class KeyWordRedactor : Form
    {
        private List<CheckBox> checkBoxList = new List<CheckBox>();
        private List<Label> labelList = new List<Label>();
        private Dictionary<string, Label> typeList = new Dictionary<string, Label>();
        Color col = new Color();
        Lexer myLex = new Lexer();
        public KeyWordRedactor()
        {
            InitializeComponent();
            colorDialog1.Color = Color.Blue;
            outButton_Click(null, null);

        }

        private void addButton_Click(object sender, EventArgs e)
        {
            bool flag = true;
            //foreach (char ch in textBox1.Text)
            //{
            //    if (!Char.IsLetterOrDigit(ch))
            //    {
            //        flag = false;
            //    }
            //}
            if (!flag)
            {
                ExceptionForm exForm = new ExceptionForm("Used unallowed symbol in keyword name");
                exForm.Show();
            }
            else
            {
                myLex.AddKeyWord(textBox1.Text, VarRadioButton.Checked, col);
                outButton_Click(sender, e);
            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("KeyWords.tdf", FileMode.Create);
            fs.Close();
            myLex = new Lexer();
            groupBox1.Controls.Clear();
        }

        private void outButton_Click(object sender, EventArgs e)
        {
            groupBox1.Controls.Clear();
            labelList = new List<Label>();
            typeList = new Dictionary<string, Label>();
            checkBoxList = new List<CheckBox>();
            //richTextBox1.Clear();
            Dictionary<string, ILexemType> sList = myLex.GetKeyWords();
            //foreach (string str in sList)
            //{
            //    richTextBox1.Text += (str.Remove(0, 1) + "\n");
            //}
            int i = 0;
            foreach (string key in sList.Keys)
            {
                if (key.Length > 1)
                {
                    checkBoxList.Add(new CheckBox());
                    groupBox1.Controls.Add(checkBoxList[i]);
                    checkBoxList[i].Top = (i + 1) * 20;
                    checkBoxList[i].Left = 20;
                    checkBoxList[i].Width = 20;
                    if (sList[key] is OtherKeyWord)
                    {
                        checkBoxList[i].Checked = ((OtherKeyWord)sList[key]).Colored;
                    }
                    else
                    {
                        checkBoxList[i].Checked = true;
                    }
                    labelList.Add(new Label());
                    groupBox1.Controls.Add(labelList[i]);
                    labelList[i].AutoSize = true;
                    labelList[i].ForeColor = Color.Black;
                    labelList[i].Text = key.Substring(1);
                    labelList[i].Top = (i + 1) * 20 + 5;
                    labelList[i].Left = 50;
                    if (!(sList[key] is OtherKeyWord))
                    {
                        checkBoxList[i].Enabled = false;
                    }
                    else
                    {
                        if (!typeList.ContainsKey(key))
                        {
                            typeList.Add(key, new Label());
                        }
                        checkBoxList[i].Enabled = true;
                        groupBox1.Controls.Add(typeList[key]);
                        if (((OtherKeyWord)sList[key]).IsVariable)
                        {
                            typeList[key].Text = "variable";
                        }
                        else
                        {
                            typeList[key].Text = "keyword";
                        }
                        typeList[key].Top = (i + 1) * 20 + 5;
                        typeList[key].Left = 100 + labelList[i].Width;
                    }
                    checkBoxList[i].CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
                    i++;
                }
            }
        }


        private void removeButton_Click(object sender, EventArgs e)
        {
            myLex.RemoveKeyWord(textBox1.Text);
            outButton_Click(sender, e);
        }

        private void colorButton_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            col = colorDialog1.Color;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            int i = 0;
            for (int j = 0; j < checkBoxList.Count; j++)
            {
                if (checkBoxList[j] == (CheckBox)sender)
                {
                    i = j;
                    break;
                }
            }
            if (((CheckBox)sender).Checked)
            {
                myLex.Colorization(true, labelList[i].Text);
            }
            else
            {
                myLex.Colorization(false, labelList[i].Text);
            }
            outButton_Click(null, e);
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {

        }

        private void typeRadioButton_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
